package com.sumainfor.pc.market.controller.rentadviser;

import com.sumainfor.common.config.RedisUtils;
import com.sumainfor.common.util.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能电表（蜂电 or 大华 ）
 */
@RestController
@RequestMapping("/rentadviser/RdInteBee")
public class RdInteBeeController implements Serializable {
    @Autowired
    private RedisUtils redisUtils;
    /**
     * 获取蜂电电表当前读数
     */
    @PostMapping("/getInteDeviceBeeList")
    public JsonResult getInteDeviceBeeList(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(params.get("InteDeviceBeeList")==null || params.get("Intelligent")==null){
            return result.putFailed("服务器繁忙");
        }
        System.out.println("接收到的参数:"+params);
        //保存匹配到第三方返回的电表当前读数
        List<Map<String,Object>> InteDeviceBeeList=(List<Map<String,Object>>)params.get("InteDeviceBeeList");
        //Intelligent 智能设备供应商     0.蜂电   1.大华   2.科技侠   3.果加
        if (params.get("Intelligent").toString().equals("0")){
            String InteDeviceBeeToken=redisUtils.get("InteDeviceBeeToken");
            //如果获取不到就重新登录
            if (InteDeviceBeeToken==null){
                Map<String,Object> loginMap=new HashMap<String, Object>();
                loginMap.put("version", UUIDUtils.generateShortUuid());//业务流水号UUID
                loginMap.put("appid",InteDeviceBeeConfig.APP_ID);
                //登录获取token
                loginMap=InteDeviceBeeUtil.userLogin(InteDeviceBeeConfig.USERID, InteDeviceBeeConfig.PASS,loginMap);
                if (loginMap.get("Code").toString().equals("0")){//0 状态码 请求成功
                    InteDeviceBeeToken=loginMap.get("Expand").toString();
                    //token存到redis中 蜂电token 有效时长一小时
                    redisUtils.set("InteDeviceBeeToken",InteDeviceBeeToken,3600);
                }else{
                    return result.putFailed("token获取失败");
                }
            }
            //组装智能电表的设备编号数组
//            InteDeviceBeeList=(List<Map<String,Object>>)params.get("InteDeviceBeeList");
            //智能电表必定是每个房间都有一个电表，用房间集合的长度初始化智能电表设备编号数组
            List<String> InteList=new ArrayList<String>();
            Map<String,Object> headMap=new HashMap<String,Object>();
            headMap.put("uid", InteDeviceBeeConfig.UID);//唐巢在蜂电用户ID
            headMap.put("token",InteDeviceBeeToken);//访问令牌
            headMap.put("version","0116010101");//版本号默认0116010101
            for(int i=0;i<InteDeviceBeeList.size();i++){
                Map<String,Object> map=InteDeviceBeeList.get(i);
                if (map.get("Identifier")!=null){
                    InteList.add(map.get("Identifier").toString());
                }
            }
            //蜂电返回的参数
            Map<String,Object> InteDataMap=InteDeviceBeeUtil.deviceAmmeterPcode(InteList,headMap);
            if (InteDataMap.get("Code").toString().equals("0")){// 0蜂电接口返回正常
                //电表数组
                List<Map<String,Object>> InteDataList=(List<Map<String,Object>>)InteDataMap.get("Data");
                for (Map<String,Object> map1:InteDeviceBeeList){
                    if (map1.get("Identifier")!=null){
                        for (Map<String,Object> map2:InteDataList){
                            if (map2.get("Ccode")!=null){
                                if (map1.get("Identifier").toString().equals(map2.get("Pcode").toString())){
                                    Map<String,Object>aMap=(Map<String,Object>)map2.get("Expand");
                                    map1.put("allpower",aMap.get("allpower"));//获取电表读数
                                }
                            }
                        }
                    }
                }
                result.put(InteDeviceBeeList);
            }else{
                return result.putFailed("第三方服务器繁忙");
            }
        }else if (params.get("Intelligent").toString().equals("1")){
            String InteDaHuaToken=redisUtils.get("InteDaHuaToken");
            //如果获取不到就重新登录
            if (InteDaHuaToken==null){
                /*获取token*/
                Map<String,Object> DaParams=new HashMap<String, Object>();
                DaParams.put("pw",InteDeviceDaHuaConfig.PW);
                DaParams.put("userName",InteDeviceDaHuaConfig.USERNAME);
                DaParams=InteDeviceDaHuaUtil.getToken(DaParams);
                if (DaParams.get("code")!=null && DaParams .get("code").toString().equals("1")){//1 状态码 请求成功
                    InteDaHuaToken=DaParams.get("data").toString();
                    //token有效期2个小时，所有接口Code=0 ，token失效 需要重新获取
                    redisUtils.set("InteDaHuaToken",InteDaHuaToken,3600*2);
                }else{
                    return result.putFailed("token获取失败");
                }
            }
            //保存智能设备编号
            String [] InteArr =new String[InteDeviceBeeList.size()];
            for(int i=0;i<InteDeviceBeeList.size();i++){
                Map<String,Object> map=InteDeviceBeeList.get(i);
                if (map.get("Identifier")!=null && !map.get("Identifier").toString().equals("")){
                    InteArr[i]=(map.get("Identifier").toString());
                }
            }
            //逗号隔开的智能设备字符串
            String level1= StringUtils.join(InteArr, ",");
            Map<String,Object> getParams=new HashMap<String,Object>();
            getParams.put("token",InteDaHuaToken);//访问token
            getParams.put("level1",level1);//逗号隔开的字符串
            //查询智能设备的电表读数
            getParams=InteDeviceDaHuaUtil.getAllRemainByLevel1(getParams);
            if (getParams.get("code")!=null && getParams .get("code").toString().equals("1")) {//0 状态码 请求成功
                //电表数组
                List<Map<String,Object>> dataList=(List<Map<String,Object>>)getParams.get("data");
                System.out.println("dataList:"+dataList);
                System.out.println("InteDeviceBeeList:"+InteDeviceBeeList);
                for (Map<String,Object> map1:InteDeviceBeeList){
                    if (map1.get("Identifier")!=null){
                        for (Map<String,Object> map2:dataList){
                            if (map2.get("meter_id")!=null){
                                if (map1.get("Identifier").toString().equals(map2.get("meter_id").toString())){
                                    map1.put("allpower",map2.get("total_e"));//获取电表读数
                                }
                            }
                        }
                    }
                }
                result.put(InteDeviceBeeList);
            }else{
                return result.putFailed("第三方服务器繁忙");
            }
        }
        return result;
    }

    /**
     * 当前电表余额清零
     */
    @PostMapping("/getBalanceEle")
    public JsonResult getBalanceEle(@RequestBody Map<String,Object> params){
        JsonResult result=new JsonResult();
        if(params.get("InteDeviceBeeList")==null || params.get("Intelligent")==null){
            return result.putFailed("服务器繁忙");
        }
        System.out.println("接收到的参数:"+params);
        //保存匹配到第三方返回的电表当前读数
        List<Map<String,Object>> InteDeviceBeeList=(List<Map<String,Object>>)params.get("InteDeviceBeeList");
        //Intelligent 智能设备供应商     0.蜂电   1.大华   2.科技侠   3.果加
        if (params.get("Intelligent").toString().equals("0")){
            String InteDeviceBeeToken=redisUtils.get("InteDeviceBeeToken");
            //如果获取不到就重新登录
            if (InteDeviceBeeToken==null){
                Map<String,Object> loginMap=new HashMap<String, Object>();
                loginMap.put("version", UUIDUtils.generateShortUuid());//业务流水号UUID
                loginMap.put("appid",InteDeviceBeeConfig.APP_ID);
                //登录获取token
                loginMap=InteDeviceBeeUtil.userLogin(InteDeviceBeeConfig.USERID, InteDeviceBeeConfig.PASS,loginMap);
                if (loginMap.get("Code").toString().equals("0")){//0 状态码 请求成功
                    InteDeviceBeeToken=loginMap.get("Expand").toString();
                    //token存到redis中 蜂电token 有效时长一小时
                    redisUtils.set("InteDeviceBeeToken",InteDeviceBeeToken,3600);
                }else{
                    return result.putFailed("token获取失败");
                }
            }
            //组装智能电表的设备编号数组
//            InteDeviceBeeList=(List<Map<String,Object>>)params.get("InteDeviceBeeList");
            //智能电表必定是每个房间都有一个电表，用房间集合的长度初始化智能电表设备编号数组
            List<String> InteList=new ArrayList<String>();
            Map<String,Object> headMap=new HashMap<String,Object>();
            headMap.put("uid", InteDeviceBeeConfig.UID);//唐巢在蜂电用户ID
            headMap.put("token",InteDeviceBeeToken);//访问令牌
            headMap.put("version","0116010101");//版本号默认0116010101
            for(int i=0;i<InteDeviceBeeList.size();i++){
                Map<String,Object> map=InteDeviceBeeList.get(i);
                if (map.get("Identifier")!=null){
                    InteList.add(map.get("Identifier").toString());
                }
            }
            //蜂电返回的参数
            Map<String,Object> InteDataMap=InteDeviceBeeUtil.deviceAmmeterPcode(InteList,headMap);
            if (InteDataMap.get("Code").toString().equals("0")){// 0蜂电接口返回正常
                //电表数组
                List<Map<String,Object>> InteDataList=(List<Map<String,Object>>)InteDataMap.get("Data");
                for (Map<String,Object> map1:InteDeviceBeeList){
                    if (map1.get("Identifier")!=null){
                        for (Map<String,Object> map2:InteDataList){
                            if (map2.get("Ccode")!=null){
                                if (map1.get("Identifier").toString().equals(map2.get("Pcode").toString())){
                                    Map<String,Object>aMap=(Map<String,Object>)map2.get("Expand");
                                    map1.put("allpower",aMap.get("allpower"));//获取电表读数
                                }
                            }
                        }
                    }
                }
                result.put(InteDeviceBeeList);
            }else{
                return result.putFailed("第三方服务器繁忙");
            }
        }else if (params.get("Intelligent").toString().equals("1")){
            String InteDaHuaToken=redisUtils.get("InteDaHuaToken");
            //如果获取不到就重新登录
            if (InteDaHuaToken==null){
                /*获取token*/
                Map<String,Object> DaParams=new HashMap<String, Object>();
                DaParams.put("pw",InteDeviceDaHuaConfig.PW);
                DaParams.put("userName",InteDeviceDaHuaConfig.USERNAME);
                DaParams=InteDeviceDaHuaUtil.getToken(DaParams);
                if (DaParams.get("code")!=null && DaParams .get("code").toString().equals("1")){//1 状态码 请求成功
                    InteDaHuaToken=DaParams.get("data").toString();
                    //token有效期2个小时，所有接口Code=0 ，token失效 需要重新获取
                    redisUtils.set("InteDaHuaToken",InteDaHuaToken,3600*2);
                }else{
                    return result.putFailed("token获取失败");
                }
            }
            //保存智能设备编号
            String [] InteArr =new String[InteDeviceBeeList.size()];
            for(int i=0;i<InteDeviceBeeList.size();i++){
                Map<String,Object> map=InteDeviceBeeList.get(i);
                if (map.get("Identifier")!=null && !map.get("Identifier").toString().equals("")){
                    InteArr[i]=(map.get("Identifier").toString());
                }
            }
            //逗号隔开的智能设备字符串
            String level1= StringUtils.join(InteArr, ",");
            Map<String,Object> getParams=new HashMap<String,Object>();
            getParams.put("token",InteDaHuaToken);//访问token
            getParams.put("level1",level1);//逗号隔开的字符串
//            //查询智能设备的电表读数
//            getParams=InteDeviceDaHuaUtil.getAllRemainByLevel1(getParams);
//            if (getParams.get("code")!=null && getParams .get("code").toString().equals("1")) {//0 状态码 请求成功
//                //电表数组
//                List<Map<String,Object>> dataList=(List<Map<String,Object>>)getParams.get("data");
//                System.out.println("dataList:"+dataList);
//                System.out.println("InteDeviceBeeList:"+InteDeviceBeeList);
//                for (Map<String,Object> map1:InteDeviceBeeList){
//                    if (map1.get("Identifier")!=null){
//                        for (Map<String,Object> map2:dataList){
//                            if (map2.get("meter_id")!=null){
//                                if (map1.get("Identifier").toString().equals(map2.get("meter_id").toString())){
//                                    map1.put("allpower",map2.get("total_e"));//获取电表读数
//                                }
//                            }
//                        }
//                    }
//                }
//                result.put(InteDeviceBeeList);
//            }else{
//                return result.putFailed("第三方服务器繁忙");
//            }

            //当前智能设备清零
            getParams = InteDeviceDaHuaUtil.clearIntefer(getParams);
            System.out.println(getParams);


        }
        return result;

    }








}
